// src/router/index.js

import { createRouter, createWebHashHistory } from 'vue-router'
import BasicLayout from '@/components/basic-layout/index.vue'
import i18n from '@/lang/index.js'

const { t } = i18n.global

const router = createRouter({
 history: createWebHashHistory(),  // hash 模式
 // history: createWebHistory(),  // history 模式
 routes: [
  {
   path: '/',
   name: 'home',
   redirect: '/portal'
  },
  {
   path: '/portal',
   name: 'portal',
   component: BasicLayout,
   meta: {
    title: 'page.home',
   },
   redirect: '/portal/home',
   children: [
    {
     path: '/portal/home',
     name: 'portal-home',
     component: () => import(`@/views/home/home.vue`),
     meta: {
      title: 'page.home',
     },
    },
    {
     path: '/portal/product-center',
     name: 'portal-product-center',
     component: () => import(`@/views/product-center/index.vue`),
     meta: {
      title: 'page.productCenter',
     },
    },
    {
     path: '/portal/service-center',
     name: 'portal-service-center',
     component: () => import(`@/views/service-center/index.vue`),
     meta: {
      title: 'page.serviceCenter',
     },
    },
    {
     path: '/portal/case-display',
     name: 'portal-case-display',
     component: () => import(`@/views/case-display/index.vue`),
     meta: {
      title: 'page.caseDisplay',
     },
    },
    {
     path: '/portal/news-center',
     name: 'portal-news-center',
     component: () => import(`@/views/news-center/index.vue`),
     meta: {
      title: 'page.newsCenter',
     },
    },
    {
     path: '/portal/about-us',
     name: 'portal-about-us',
     component: () => import(`@/views/about-us/index.vue`),
     meta: {
      title: 'page.aboutUs',
     },
    },
    {
     path: '/portal/ecological-partner',
     name: 'portal-ecological-partner',
     component: () => import(`@/views/ecological-partner/index.vue`),
     meta: {
      title: 'page.ecologicalPartner',
     },
    }
   ]
  },
  {
   path: '/*',
   redirect: '/',
  },
 ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
 const appTitle = t(import.meta.env.VITE_APP_TITLE)
 const pageTitle = t(to.meta.title)
 if (to.meta.title) {
  document.title = `${appTitle} - ${pageTitle}`
 }
 next()
})

router.afterEach((to, from) => {
 // console.log(to, from)
 console.log('afterEach')
})

export default router